package cn.wang.heng.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.wang.heng.make.LianJieShuJuKu;
import cn.wang.heng.make.ShuJuCaoZuo;
import cn.wang.heng.text.ShangPin;
import cn.wang.heng.text.index;

public class IndexShangBao extends HttpServlet {
	private ArrayList shangpinlist=new ArrayList();
	/**
	 * Constructor of the object.
	 */
	public IndexShangBao() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
		
		 

		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String path = request.getContextPath();
		HttpSession session=request.getSession();
		
		//session.setAttribute("shangpinlist",null);
		int a=1;
		int b=0;
		
		//if(b==0)
			b=b+shangpinlist.size();
		
		if(shangpinlist.size()==0 || shangpinlist==null)
			a=0;
		index chan=new index();
		chan.setChan(b);
		session.setAttribute("chan", chan);
		if(a==1)
		{
			session.setAttribute("shangpinlist",null);
		}
	
		
		int goodid;
		int goodaccount;
		String goodname;
		float goodprice;
		String goodpicture;
		String goodintroduce;
		
		String sql="select * from good";
		ShuJuCaoZuo shuju=new ShuJuCaoZuo();
		ResultSet rs=shuju.Cha1(sql);
		int i=0;
		try {
			while(rs.next()){
				ShangPin goods=new ShangPin();
				goodid=rs.getInt(1);
				goods.setGoodid(goodid);
				
				goodaccount=rs.getInt(2);
				goods.setGoodaccount(goodaccount);
				
				goodname=rs.getString(3);
				goods.setGoodname(goodname);
				
				goodprice=rs.getFloat(4);
				goods.setGoodprice(goodprice);
				
				goodpicture=rs.getString(5);
				goods.setGoodpicture(goodpicture);
				
				goodintroduce=rs.getString(6);
				goods.setGoodintroduce(goodintroduce);
				
				shangpinlist.add(goods);
				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//LianJieShuJuKu mysql=new LianJieShuJuKu();
		
		try {
			rs.close();
			//conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.setAttribute("shangpinlist", shangpinlist);
		response.setHeader("Refresh", "0;URL=show.jsp");
		
	//	response.sendRedirect(path+"/show.jsp");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
